#include "globalconfig.h"
#include <QSettings>
#include <QUuid>
#include <QHostInfo>
#include <QDebug>
#include <QDir>

GlobalConfig::GlobalConfig()
{
    QSettings setting("./globalConfig.ini", QSettings::IniFormat);
    setting.beginGroup("User");
    m_tcpPort = setting.value("tcpPort").toUInt();
    m_udpPort = setting.value("udpPort").toUInt();
    m_broadcastPort = setting.value("broadcastPort").toUInt();

    m_uuid = setting.value("uuid").toString();
    m_name = setting.value("name").toString();
    m_autograph = setting.value("autograph").toString();
    m_friendPhotoPath = setting.value("friendPhotoPath").toString();
    m_userPhotoPath = setting.value("userPhotoPath").toString();
    m_downloadPath = setting.value("downloadPath").toString();

    if(m_uuid.isEmpty() || m_uuid == "null"){
        m_uuid = QUuid::createUuid().toString();
        m_uuid.remove('-');
        m_uuid.remove('{');
        m_uuid.remove('}');
        setting.setValue("uuid", m_uuid);
    }

    if(m_name.isEmpty() || m_name == "null"){
        m_name = QHostInfo::localHostName();
        setting.setValue("name", m_name);
    }

    if(m_name.isEmpty() || m_autograph == "null"){
        m_autograph = "不积跬步,无以至千里";
        setting.setValue("autograph", m_autograph);
    }

    QFileInfo fileInfo(m_userPhotoPath);
    if(!fileInfo.exists()){
        m_userPhotoPath = ":/images/icon/Chat.png";
        setting.setValue("userPhotoPath", m_userPhotoPath);
    }
    //如果没有设置下载路径则默认在软件所在目录建立下载目录
    QDir dirs(m_downloadPath);
    if(m_downloadPath == "null" || !dirs.exists()){
        QString currentPath = QDir::currentPath();
        QDir dir(currentPath + "/download");
        if(!dir.exists()){
            if(!dir.mkdir(currentPath + "/download")){
                qDebug() << QString("创建下载目录%1失败").arg(m_downloadPath);
                exit(1);
            }
        }
        setting.setValue("downloadPath", dir.absolutePath());
    }

    setting.endGroup();


    qDebug() << "m_tcpPort = " << m_tcpPort;
    qDebug() << "m_udpPort = " << m_udpPort;
    qDebug() << "m_broadcastPort = " << m_broadcastPort;
    qDebug() << "m_userPhotoPath = " << m_userPhotoPath;
    qDebug() << "m_friendPhotoPath = " << m_friendPhotoPath;
    qDebug() << "m_downloadPath = " << m_downloadPath;
    qDebug() << "m_uuid = " << m_uuid;
    qDebug() << "m_name = " << m_name;
}

void GlobalConfig::setTcpPort(quint16 port)
{
    QSettings setting("./globalConfig.ini", QSettings::IniFormat);
    setting.beginGroup("User");
    setting.setValue("tcpPort", port);
    setting.endGroup();
}

void GlobalConfig::setUdpPort(quint16 port)
{
    QSettings setting("./globalConfig.ini", QSettings::IniFormat);
    setting.beginGroup("User");
    setting.setValue("udpPort", port);
    setting.endGroup();
}

void GlobalConfig::setBroadcastPort(quint16 port)
{
    QSettings setting("./globalConfig.ini", QSettings::IniFormat);
    setting.beginGroup("User");
    setting.setValue("broadcastPort", port);
    setting.endGroup();
}

void GlobalConfig::setName(const QString &name)
{
    QSettings setting("./globalConfig.ini", QSettings::IniFormat);
    setting.beginGroup("User");
    setting.setValue("name", name);
    setting.endGroup();
}

void GlobalConfig::setAutograph(const QString &autograph)
{
    QSettings setting("./globalConfig.ini", QSettings::IniFormat);
    setting.beginGroup("User");
    setting.setValue("autograph", autograph);
    setting.endGroup();
}

void GlobalConfig::setUserPhotoPath(const QString &path)
{
    QSettings setting("./globalConfig.ini", QSettings::IniFormat);
    setting.beginGroup("User");
    setting.setValue("userPhotoPath", path);
    setting.endGroup();
}

void GlobalConfig::setDownloadPath(const QString &path)
{
    QSettings setting("./globalConfig.ini", QSettings::IniFormat);
    setting.beginGroup("User");
    setting.setValue("downloadPath", path);
    setting.endGroup();
}
